﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace KZ400B
{
    /// <summary>
    /// 参数保留
    /// </summary>
    public partial class AxleManager
    {
        /// <summary>
        /// 参数保留
        /// </summary>
        /// <returns></returns>
        public static bool VZero(string axleName)
        {
            if (!IsOpen)
            {
                return false;
            }
            string data = Protocol.Packet(
                Protocol.GetVZero(axleName)
                );
            return sendData(data);
        }

        /// <summary>
        /// 停止
        /// </summary>
        /// <param name="axleName"></param>
        /// <returns></returns>
        public static bool VZeroIM(string axleName)
        {
            bool rst = VZero(axleName);
            if (rst)
            {
                lock (s_spLocker)
                {
                    if (spLink.CanRead)
                    {
                        string msg = spLink.ReadString(10);
                        if (string.IsNullOrEmpty(msg))
                        {
                            return false;
                        }
                        ProtocolV2.UnpacketResult ur = ProtocolV2.Unpack(msg);
                        rst = ur.Result && ur.Action == AxleActions.VZeroRespond;
                        if (!rst)
                        {
                            return rst;
                        }
                        OnToZeroRespond(ur);



                        ///虚拟零点设置或者
                        msg = spLink.ReadString(240);
                        if (string.IsNullOrEmpty(msg))
                        {
                            return false;
                        }
                        ur = ProtocolV2.Unpack(msg);
                        rst = ur.Result;
                        if (!rst)
                        {
                            return false;
                        }
                        if (ur.Action == AxleActions.VZeroSetFinish)
                        {
                            OnVZeroSetComplete(ur);
                        }
                        else if (ur.Action == AxleActions.VZeroRemoveFinish)
                        {
                            OnVZeroRmComplete(ur);
                        }
                        else if (ur.Action == AxleActions.VZeroFinish)
                        {
                            OnVZeroComplete(ur,true);
                        }
                        else
                        {
                            return false;
                        }

                        ///position info
                        msg = spLink.ReadString(240);
                        if (string.IsNullOrEmpty(msg))
                        {
                            return false;
                        }
                        ur = ProtocolV2.Unpack(msg);
                        rst = ur.Result && ur.Action == AxleActions.QueryPositionFinish;
                        if (!rst)
                        {
                            return false;
                        }
                        
                        return true;
                    }
                }// link lock
            }
            return rst;
        }

        static void OnVZeroRespond(ProtocolV2.UnpacketResult ur)
        {
            if (ur.Result
                    && ur.Action == AxleActions.VZero

                    )
            {
                if (AxleCacheManager.AxleList.ContainsKey(ur.AxleName))
                {
                    AxleCacheManager.AxleList[ur.AxleName].AxleStatus = AxleStatus.Running;

                }
            }

            setState(
                string.Format("[{0}] 轴 设置虚拟零位命令响应 : {1} {2} "
                        , ur.AxleName
                        , ur.Result ? "正确" : "错误"
                        , ur.Result ? "-> 设置虚拟零位运行中 ... " : string.Empty
                        ));
        }

        static void OnVZeroComplete(ProtocolV2.UnpacketResult ur)
        {
            OnVZeroComplete(ur, false);
        }

        static void OnVZeroComplete(ProtocolV2.UnpacketResult ur,bool im)
        {
            if (ur.Result
                && ur.Action == AxleActions.VZeroFinish

                )
            {

                AxleRunState.Instance[ur.AxleName] = false;

                if (AxleCacheManager.AxleList.ContainsKey(ur.AxleName))
                {

                    //if (im)
                    //{ QueryPositionIM(ur.AxleName); }
                    //else
                    //{
                    //    QueryPosition(ur.AxleName);
                    //}
                    //Thread.Sleep(100);
                }

                if (AxleCacheManager.AxleList.ContainsKey(ur.AxleName))
                {

                }

            }
            setState(
                string.Format("[{0}] 轴 设置虚拟零位命令运行结束 : {1}"
                        , ur.AxleName
                        , ur.Result ? "正确" : "错误"
                        )
                        );
        }

        static void OnVZeroSetComplete(ProtocolV2.UnpacketResult ur)
        {
            OnVZeroSetComplete(ur, false);
         }

        static void OnVZeroSetComplete(ProtocolV2.UnpacketResult ur,bool im)
        {
            if (ur.Result
                    && ur.Action == AxleActions.VZeroSetFinish

                    )
            {

                AxleRunState.Instance[ur.AxleName] = false;

                if (AxleCacheManager.AxleList.ContainsKey(ur.AxleName))
                {
                    AxleCacheManager.AxleList[ur.AxleName].AxleActionStatus.VZeroIsSeted = true;

                    //if (im)
                    //{ QueryPositionIM(ur.AxleName); }
                    //else
                    //{
                    //    QueryPosition(ur.AxleName);
                    //}
                    //Thread.Sleep(100);
                }

            }
            setState(
                string.Format("[{0}] 轴 设置虚拟零位设置命令运行结束 : {1}"
                        , ur.AxleName
                        , ur.Result ? "正确" : "错误"
                        )
                        );
        }


        static void OnVZeroRmComplete(ProtocolV2.UnpacketResult ur)
        {
            OnVZeroRmComplete(ur, false);
        }

        static void OnVZeroRmComplete(ProtocolV2.UnpacketResult ur, bool im)
        {
            if (ur.Result
                    && ur.Action == AxleActions.VZeroRemoveFinish

                    )
            {

                AxleRunState.Instance[ur.AxleName] = false;

                if (AxleCacheManager.AxleList.ContainsKey(ur.AxleName))
                {
                    AxleCacheManager.AxleList[ur.AxleName].AxleActionStatus.VZeroIsSeted = false;

                    //if (im)
                    //{ QueryPositionIM(ur.AxleName); }
                    //else
                    //{
                    //    QueryPosition(ur.AxleName);
                    //}
                    //Thread.Sleep(100);
                }

            }
            setState(
                string.Format("[{0}] 轴 设置虚拟零位取消命令运行结束 : {1}"
                        , ur.AxleName
                        , ur.Result ? "正确" : "错误"
                        )
                        );
        }

        static void OnVZeroFailedComplete(ProtocolV2.UnpacketResult ur)
        {
            OnVZeroFailedComplete(ur, false);
        }

        static void OnVZeroFailedComplete(ProtocolV2.UnpacketResult ur, bool im)
        {
            if (ur.Result
                    && ur.Action == AxleActions.VZeroFailed

                    )
            {

                AxleRunState.Instance[ur.AxleName] = false;

                if (AxleCacheManager.AxleList.ContainsKey(ur.AxleName))
                {
                    AxleCacheManager.AxleList[ur.AxleName].AxleActionStatus.VZeroIsSeted = false;

                    //if (im)
                    //{ QueryPositionIM(ur.AxleName); }
                    //else
                    //{
                    //    QueryPosition(ur.AxleName);
                    //}
                    //Thread.Sleep(100);
                }

            }
            setState(
                string.Format("[{0}] 轴 虚拟零位命令运行失败"
                        , ur.AxleName
                        )
                        );
        }
    }
}
